class Sperr // KLASSENNAME
{
  // INSTANZVARIABLEN

  float U; // Spannung in Volt
  float IR; // Teilspannung in Volt
  float IL; // Teilspannung in Volt
  float IC; // Teilspannung in Volt
  float R; // ohmscher Widerstand in Ohm
  float RL; // induktiver Widerstand in Ohm
  float RC; // kapazitiver Widerstand in Ohm
  float Z; // Wechselstromwiderstand der Siebkette
  float I; // Strom in Ampere
  float f; // Frequenz in Hertz
  float L; // Induktivität in Henry
  float C; // Kapazität in Farad

  // KONSTRUKTOR

  Sperr(float Rtemp, float Ltemp, float Ctemp)
  {
    R = Rtemp;
    L = Ltemp;
    C = Ctemp;
  }

  // METHODEN (FUNKTIONEN)

  void schaltung()
  {
    translate(0, 100);

    // Die Leitungen des Stromkreises werden gezeichnet
    stroke(0);
    strokeWeight(3);
    noFill();
    rect(104, 183, 205, 198);

    beginShape();
    vertex(410, 300);
    vertex(410, 425);
    vertex(207, 425);
    vertex(208, 45);
    vertex(410, 45);
    vertex(410, 211);
    endShape(); 

    // Kontakte
    fill(255);
    ellipse(207, 380, 10, 10);
    ellipse(207, 184, 10, 10);
    ellipse(410, 300, 10, 10);
    ellipse(410, 217, 10, 10);

    // Strommessgerät
    ellipse(209, 119, 80, 80);

    // Widerstand R
    stroke(0);
    strokeWeight(3);
    fill(0);
    rect(80, 229, 48, 106);

    // Widerstand RL
    stroke(0);
    strokeWeight(3);
    fill(255);
    rect(181, 230, 48, 106);

    // Widerstand RC
    stroke(0);
    strokeWeight(3);
    fill(0);
    rect(268, 259, 84, 40);
    stroke(255);
    strokeWeight(17);
    line(265, 279, 355, 279);
  }

  void messwerte(float U, float f1)
  {
    // Berechnung von RL, RC und Z
    RL = 2*PI*f1*L;
    RC = 1/(2*PI*f1*C);
    Z = 1/(sqrt(1/pow(R, 2) + pow((1/RC-1/RL), 2)));

    // Berechnung von I und den Teilspannungen UR, UL und UC
    I = U/Z;
    IR = U/R;
    IL = U/RL;
    IC = U/RC;

    // Beschriftung
    fill(0);
    textSize(20);
    text("f in Hz = " +f1, 370, 281);
    text("U\u007E in V = " +U, 370, 253);
    text("IR in A = " +(float)round(100*IR)/100, 573, 390);
    text("IL in A = " +(float)round(100*IL)/100, 573, 340);
    text("IC in A = " +(float)round(100*IC)/100, 573, 365);
    text("R in \u03A9 = " +R, 143, -10);
    text("RL in \u03A9 = " +(float)round(10*RL/10), 59, 26);
    text("RC in \u03A9 = " +(float)round(10*RC/10), 247, 26);
    text("" +(float)round(100*I)/100, 184, 128);
    text("I in A", 110, 128);
    textSize(34);
    fill(0, 0, 255);
    text("Z in \u03A9 = " +(float)round(10*Z/10), 501, 435);

    // Zum Zeichnen des Zeigerdiagramms wird der Koordinatenursprung verschoben
    translate(550, 150);

    // Rechteck
    noFill();
    stroke(0);
    strokeWeight(1);
    rect(0, 0, 100*IR, (-100*IC + 100*IL));

    // Zeiger für  U
    stroke(0, 255, 0);
    strokeWeight(4);
    line(0, 0, U, 0);
    triangle(U-10, -3, U, 0, U-10, 3);

    // Zeiger für IR, IL, IC, IL - IC und I
    stroke(0);
    strokeWeight(3);
    line(0, 0, 100*IR, 0);
    strokeWeight(3);
    triangle(100*IR-10, -3, 100*IR, 0, 100*IR-10, 3);
    stroke(255, 0, 255);
    strokeWeight(2);
    line(0, 0, 0, -100*IC);
    triangle(-3, -100*IC+10, 0, -100*IC, 3, -100*IC+10);
    stroke(0, 0, 255);
    strokeWeight(2);
    line(0, 0, 0, 100*IL);
    triangle(-3, 100*IL-10, 0, 100*IL, 3, 100*IL-10);
    stroke(255, 0, 255);
    strokeWeight(3);
    line(-10, 100*IL, -10, -100*IC+100*IL);
    triangle(-13, -100*IC+8+100*IL, -10, -100*IC+100*IL, -7, -100*IC+8+100*IL);
    stroke(255, 0, 0);
    line(0, 0, 100*IR, (-100*IC + 100*IL)); // U
    ellipse(100*IR, (-100*IC + 100*IL), 5, 5); // Kinderpfeilspitze

    // Texte
    textSize(24);
    fill(255, 0, 255);
    text("IC,", 22, 150);
    fill(0, 0, 255);
    text("IL,", 65, 150);
    fill(0);
    text("IR,", 109, 150);
    fill(255, 0, 0);
    text("I,", 152, 150);
    fill(0, 255, 0);
    text("U", 183, 150);
  }

  void sinus(float f2)
  {
    /* Zum Zeichnen der I(t)- und U(t)-Funktion wird der Koordinatenursprung 
     erneut verschoben */
    translate(-750, 400);

    // Berechnung der Phasenverschiebung
    float Phi = atan(R*(1/RC - 1/RL)); 

    // Sinusfunktion mit Achsen
    for (float x = 250, t = 0; x >= 250 && x <= 650; t = t + 0.0004, x = x + 1)
    {
      // Wechselstrom
      float It = 100*I*sin(2*PI*f2*t);
      fill(255, 0, 0);
      noStroke();
      ellipse(x, It, 5, 5);

      // Wechselspannung
      float Ut = Z*I*sin(2*PI*f2*t+Phi);
      fill(0, 255, 0);
      noStroke();
      ellipse(x, Ut, 5, 5);
    }
    // Koordinatensystem
    stroke(0);
    strokeWeight(5);
    line(250, 0, 650, 0);
    line(250, 100, 250, -100);
    fill(0);
    triangle(247, -80, 250, -100, 253, -80);

    // Text für U- und I-Funktion
    textSize(24);
    fill(255, 0, 0);
    text("I,", 685, -70);
    fill(0, 255, 0);
    text("U", 715, -70);
    fill(150);
    text("Phi in Grad =  " +(float)round(100*(degrees(Phi))/100), 688, -32);
  }
}